using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;

[RequireComponent(typeof(UIInput))]
public class InputLengthLimit : MonoBehaviour {
	
	UIInput myInput;
	
	void Start(){
		myInput = GetComponent<UIInput>();
	}
	
	void OnSubmit(string text){
		myInput.text = StringLengthCheck(text);
	}
	
	void OnInputChanged(UIInput mInput){
		mInput.text = StringLengthCheck(mInput.text);
	}
	
	string StringLengthCheck(string mString){
		string text = mString;
		int count = Regex.Replace(mString,@"[\u4e00-\u9fa5/g]", "aa").Length;
		if(count > 8){
			for(int i = 3;i < mString.Length;i++){
				string s = mString.Substring(0,i + 1);
				if((Regex.Replace(s,@"[\u4e00-\u9fa5/g]", "aa").Length) > 8) {
					text = mString.Substring(0,i);
					break;
				}
			}
		}
		return text;
	}
}
